package com.matriq.cloudmanager;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.LocationSource.OnLocationChangedListener;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.matriq.cloudmanager.R;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

public class MapActivity extends Activity {

	// 关于地图的两个参数
	double lat = 30.5984824;// 纬度
	double lng = 104.16638196;// 经度
	ImageButton btnBack;
	private OnLocationChangedListener mListener;
	private AMapLocationClient mlocationClient;
	private AMapLocationClientOption mLocationOption;
	private MapView mapView;
	private AMap aMap;
	Marker selectMarker;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		setViews();
		mapView.onCreate(savedInstanceState);// 此方法必须重写
		setEvents();
		loadData();
	}

	private void setViews() {
		mapView = (MapView) findViewById(R.id.gaode_map);
		btnBack = (ImageButton) findViewById(R.id.btnBack);
		if (aMap == null)
			aMap = mapView.getMap();
	}

	private void setEvents() {
		btnBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}

	private void loadData() {
		selectMarker = showMarker(lat, lng, R.drawable.main_location);
		setUpMap();
	}

	private Marker showMarker(double lat, double lng, int res) {
		MarkerOptions markerOption = new MarkerOptions();
		LatLng latlng = new LatLng(lat, lng);
		markerOption.position(latlng);
		markerOption.draggable(true);
		markerOption.icon(BitmapDescriptorFactory.fromResource(R.drawable.main_location));
		Marker mk = aMap.addMarker(markerOption);
		aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));
		return mk;
	}
	
	/**
	 * 初始化AMap对象
	 */
	LocationSource locationSource;
	/**
	 * 设置一些amap的属性
	 */
	private void setUpMap() {
		// 自定义系统定位小蓝点
		MyLocationStyle myLocationStyle = new MyLocationStyle();
		myLocationStyle.myLocationIcon(BitmapDescriptorFactory
				.fromResource(R.drawable.location_marker));// 设置小蓝点的图标
		myLocationStyle.strokeColor(Color.BLACK);// 设置圆形的边框颜色
		myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 设置圆形的填充颜色
		// myLocationStyle.anchor(int,int)//设置小蓝点的锚点
		myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细
		aMap.setMyLocationStyle(myLocationStyle);
		locationSource = new LocationSource() {
			
			@Override
			public void deactivate() {
				mListener = null;
				if (mlocationClient != null) {
					mlocationClient.stopLocation();
					mlocationClient.onDestroy();
				}
				mlocationClient = null;
			}
			
			@Override
			public void activate(OnLocationChangedListener listener) {
				mListener = listener;
				if (mlocationClient == null) {
					mlocationClient = new AMapLocationClient(MapActivity.this);
					mLocationOption = new AMapLocationClientOption();
					//设置定位监听
					mlocationClient.setLocationListener(new AMapLocationListener() {
						
						@Override
						public void onLocationChanged(AMapLocation amapLocation) {
							if (mListener != null && amapLocation != null) {
								if (amapLocation != null
										&& amapLocation.getErrorCode() == 0) {
									mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
								} else {
									String errText = "定位失败," + amapLocation.getErrorCode()+ ": " + amapLocation.getErrorInfo();
									Log.e("haha",errText);
								}
							}
						}
					});
					//设置为高精度定位模式
					mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
					//设置定位参数
					mlocationClient.setLocationOption(mLocationOption);
					// 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
					// 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
					// 在定位结束后，在合适的生命周期调用onDestroy()方法
					// 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
					mlocationClient.startLocation();
				}
			}
		};
		aMap.setLocationSource(locationSource);// 设置定位监听
		aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
		aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
	   // aMap.setMyLocationType()
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onResume() {
		super.onResume();
		mapView.onResume();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onPause() {
		super.onPause();
		mapView.onPause();
		locationSource.deactivate();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}

}
